Ext.onReady(function(){
    Ext.define('OfficeModel',{
        extend: 'Ext.data.Model',
        fields: [
			{name:'id',type:'string'},
            {name:'name',type:'string'},
            {name:'parent__name',type:'string'},
            {name:'parent__id',type:'string'}
        ]
    });
    //创建数据源
    var OfficeStore = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'OfficeModel',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/office_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
  
    //创建Grid
    var OfficeGrid = Ext.create('Ext.grid.Panel',{
        store: OfficeStore,
        //height: 500,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
            {text: "职务名称", width: 100, dataIndex: 'name', sortable: false},
            {text: "上级职务", width: 120, dataIndex: 'parent__name', sortable: false}
        ],
        layout : 'fit',
        loadMask:true,
        region: 'north',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = OfficeGrid.getSelectionModel().getSelection();
        			if(selected!=''){
        				form.getForm().reset();
        				form.loadRecord(selected[0]);
        				win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的职务!');
	    			}    
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : OfficeStore,
            dock : 'bottom',
            displayInfo : true,
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = OfficeGrid.getSelectionModel().getSelection();
	        			if(selected!=''){
	        			form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的职务!');
	    			}        			
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			
        			var selected = OfficeGrid.getSelectionModel().getSelection();
        			if(selected !=''){
	        			Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
			        			var ids=[];
			        			Ext.each(selected, function (item) {
			        				ids.push(item.data.id);
			        			});
			        			Ext.Ajax.request({
			        			    url:'/resourcecontrol/office_del/',
			        			    method : 'post',
			        			    params:{
			        			    	ids:ids.join(",")
			        			    },
			        			    success:function(response){
			        			        var results =  Ext.decode(response.responseText);
			        			        if(results.success){
			        			        	OfficeStore.load();
			        			        }else{
			        			        	Ext.Msg.alert('系统提示', results.message);
			        			        }
			        			    }
			        			});
	        				}
	        			});
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要删除的校区!');
        			}         			
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 350,
		bodyPadding: 10,
        fieldDefaults: {
            //labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'textfield',
			fieldLabel:'职务名称',
			name:'name',
			allowBlank: false
		},{
    		xtype: 'combobox',
		    name:'parent__id',
	        fieldLabel: '上级职务', 
           	emptyText: '请选择职务',
           	editable:false,
           	store: new Ext.data.Store({
               	singleton : true, 
    	   	    proxy: {
    	   	        type: 'ajax',
    	   	        url: '/resourcecontrol/office_select/',
    	   	        actionMethods:'post',
    	   	        reader: {
    	   	            type: 'json'
    	   	        }
    	   	    },
               fields:['id', 'name'],
               autoLoad:true
           	}), 
			listeners: {  
				beforequery:function(queryEvent,eOpts){
					queryEvent.combo.getStore().load();
				} 
			},        	
           	loadingText:'正在加载数据，请稍侯……', 
           	triggerAction:'all', 
           	valueField:'id', 
           	displayField:'name' 		
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						submitEmptyText:false,
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/resourcecontrol/office_save/',
						success : function(form, action) {
							win.hide();
							OfficeStore.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'office-grid',
		items:[OfficeGrid]
	});
	
});